package com.zw.pm.module.board.dailyoutputtotal;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellData;
import com.zw.pm.gen.entity.OutputDailyTotal;
import com.zw.pm.gen.mapper.OutputDailyTotalMapper;

import java.util.*;

/**
 * @Description: 生产日报表 导入 listener
 * @Author: China.wsg
 * @Date: 2020/01/09 16:38
 */
public class DailyOutputTotalListener extends AnalysisEventListener<OutputDailyTotal> {

    public DailyOutputTotalListener(OutputDailyTotalMapper outputDailyTotalMapper, Date yearMonth, String uploadPath, HandlerErrorInterface handlerErrorInterface) {
        this.outputDailyTotalMapper = outputDailyTotalMapper;
        this.handlerErrorInterface = handlerErrorInterface;
        this.uploadPath = uploadPath;

        Calendar ca = Calendar.getInstance();
        ca.setTime(yearMonth);
        this.year = ca.get(Calendar.YEAR);
        this.month = ca.get(Calendar.MONTH) + 1;
    }

    private OutputDailyTotalMapper outputDailyTotalMapper;
    private HandlerErrorInterface handlerErrorInterface;
    private Integer year;
    private Integer month;
    private DailyOutputTotalModel dailyOutputTotalModel;
    private String uploadPath;

    /**
     * 由于sqlserver 2100 参数限制， 每隔80条存储数据库，
     */
    private static final int BATCH_COUNT = 80;

    private List<OutputDailyTotal> outputDailyTotals = new ArrayList<>();       // 正常数据
    private List<DailyOutputTotalModel> dailyOutputTotalModels = new ArrayList<>();     // 异常数据

    @Override
    public void invoke(OutputDailyTotal data, AnalysisContext context) {

        data.setYear(this.year);
        data.setMonth(this.month);
        outputDailyTotals.add(data);
        if (outputDailyTotals.size() >= BATCH_COUNT) {

            for (int i = 0; i < outputDailyTotals.size(); i++) {

                // 循环插入日报表数据
                outputDailyTotalMapper.insert(outputDailyTotals.get(i));
            }
            outputDailyTotals.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (outputDailyTotals.size() > 0) {
            for (int i = 0; i < outputDailyTotals.size(); i++) {

                // 循环插入日报表数据
                outputDailyTotalMapper.insert(outputDailyTotals.get(i));
            }
        }

        // 异常数据回传到service层
        handlerErrorInterface.handler(dailyOutputTotalModels);
    }

    /**
     * 异常数据捕获
     *
     * @param exception
     * @param context
     * @throws Exception
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        dailyOutputTotalModel = new DailyOutputTotalModel();
        Map<Integer, CellData> map = (Map<Integer, CellData>) context.readRowHolder().getCurrentRowAnalysisResult();

        dailyOutputTotalModel.setYear(this.year);
        dailyOutputTotalModel.setMonth(this.month);

        for (Map.Entry<Integer, CellData> entry : map.entrySet()) {

            if (entry.getKey().compareTo(0) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setDate(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setDate(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(1) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setAutoWeldWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setAutoWeldWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(2) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setSystemBuildingWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setSystemBuildingWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(3) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setFinanceWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setFinanceWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(4) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setSpareStockWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setSpareStockWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(5) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setAluminumAngleWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setAluminumAngleWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(6) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setSwordIronWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setSwordIronWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(7) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setNoWeldWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setNoWeldWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(8) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setWeldTotalWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setWeldTotalWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(9) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setOutputTotalWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setOutputTotalWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(10) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setBoardOutWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setBoardOutWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(11) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setAluminumBackingWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setAluminumBackingWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(12) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setAluminumSupportWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setAluminumSupportWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(13) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setBolaWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setBolaWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(14) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setAluminumGasketWeight(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setAluminumGasketWeight(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(15) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setOutSourcingTotal(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setOutSourcingTotal(entry.getValue().getStringValue());
                }
            } else if (entry.getKey().compareTo(16) == 0) {
                if (entry.getValue().getType().name().compareTo("NUMBER") == 0) {
                    dailyOutputTotalModel.setBoardTotal(String.valueOf(entry.getValue().getNumberValue()));
                } else {
                    dailyOutputTotalModel.setBoardTotal(entry.getValue().getStringValue());
                }
            }
        }
        dailyOutputTotalModel.setCreateDate(new Date());
        dailyOutputTotalModel.setReason("存在非数字格式内容，导入失败。");

        // 异常数据添加到 异常数据列表中
        dailyOutputTotalModels.add(dailyOutputTotalModel);
    }


}